package edu.calpoly.android.privatecontacts;

import java.util.ArrayList;
import java.util.List;

import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;


import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.TextView;

public class StudentActivity extends Activity {
   private List<ParseObject> entries;
   private List<StudentEntry> realEntries;
   private List<ParseObject> parentIds;
   private List<ParseObject> parentObjects;
   private List<Parent> parents;
   private String table;
   private String studentId;
   //private EntryAdapter m_adapter;
   //private ListView entryView;
   ListView list;
   
   //view stuff
   private TextView name;
   //private TextView grade;
   private TextView parent1Name;
   private TextView parent2Name;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      this.setContentView(R.layout.student_entry_view);

      Bundle content = this.getIntent().getExtras();
      table = content.getString("table");
      studentId = content.getString("id");
      this.realEntries = new ArrayList<StudentEntry>();
      this.parents = new ArrayList<Parent>();
      this.entries = new ArrayList<ParseObject>();
      this.parentObjects = new ArrayList<ParseObject>();
      //this.m_adapter = new EntryAdapter(m_context, realEntries);
      //this.entryView = (ListView) findViewById(R.id.list);
      //this.entryView.setAdapter(m_adapter);
      
      //-- things for view
      this.name = (TextView) this.findViewById(R.id.name);
      this.parent1Name = (TextView) this.findViewById(R.id.parent1Name);
      this.parent2Name = (TextView) this.findViewById(R.id.parent2Name);
      
      new RemoteDataTask().execute();
      
   }

   private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
      // Override this method to do custom remote calls
      protected Void doInBackground(Void... params) {
         // Gets the current list of todos in sorted order
         ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(table);
         ArrayList<String> pIds = new ArrayList<String>();
         Log.d("prototype", "query state: " + (query == null));
         try {
            query.get(studentId);
            entries = query.find();
            query = new ParseQuery<ParseObject>("StudentXParent");
            query.whereEqualTo("studentId", entries.get(0));  // kind of weird, maybe try something else 
            parentIds = query.find();
            Log.d("prototype", "PIds size:" + parentIds.size()); 
            Log.d("prototype", "objectId: " + (String) (parentIds.get(0).getObjectId()));
            for (ParseObject id : parentIds) {
               //--This was the tricky part about dealing with pointers in the database
               pIds.add((String) ((ParseObject)id.get("parentId")).getObjectId()); 
            }
            Log.d("prototype", "objectId: " + (parentIds.get(0).getObjectId()));
            Log.d("prototype", "pIds size:" + pIds.size());
            Log.d("prototype", "pIds content:" + pIds.toString());
            query = new ParseQuery<ParseObject>("Parent");
            query.whereContainedIn("objectId", pIds);
            parentObjects = query.find();
            
            Log.d("prototype", "PO size:" + parentObjects.size());
         } catch (ParseException e) {
            Log.d("prototype", "problem initializing entries");
         }
         return null;
      }

      @Override
      protected void onPreExecute() {
         // ListActivity.this.progressDialog =
         // ProgressDialog.show(ListActivity.this, "", "Loading...", true);
         super.onPreExecute();
      }

      @Override
      protected void onProgressUpdate(Void... values) {

         super.onProgressUpdate(values);
      }

      @Override
      protected void onPostExecute(Void result) {
         for (ParseObject entry : entries) {
            Log.d("proto", "in postExecute");
            realEntries.add(new StudentEntry((String)entry.get("firstName"), (String)entry.get("lastName")));
         }
         for (ParseObject pUnit : parentObjects) {
            Log.d("proto", ((String)pUnit.get("firstName")));
            parents.add(new Parent((String)(pUnit.get("firstName")), (String)pUnit.get("lastName")));
         }
         name.setText("Student: " + realEntries.get(0).toString());
         parent1Name.setText("Parent: " + parents.get(0).toString());
         parent2Name.setText("Parent: " + (parents.size() > 1 ? parents.get(1).toString() : "N/A"));
         Log.d("proto", "finished postExecute");
      }
   }
}